# backend/models/user_subscription.py
from datetime import datetime
from sqlalchemy import Boolean, Integer, DateTime, ForeignKey, Text, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from backend.db.base import Base
import uuid


class UserSubscription(Base):
    __tablename__ = "user_subscriptions"

    id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
    user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
    subscription_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("subscriptions.id"))
    node_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("nodes.id"))

    # WireGuard 客户端配置
    wg_public_key: Mapped[str] = mapped_column(String(255), nullable=True)
    wg_private_key: Mapped[str] = mapped_column(String(255), nullable=True)
    client_ip: Mapped[str] = mapped_column(String(20), nullable=True)

    started_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    expires_at: Mapped[datetime]
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    used_traffic_mb: Mapped[int] = mapped_column(Integer, default=0)
    status_notes: Mapped[str] = mapped_column(Text, nullable=True)

    # 关系
    user = relationship("User", back_populates="user_subscriptions")
    subscription = relationship("Subscription", back_populates="user_subscriptions")
    node = relationship("Node", back_populates="user_subscriptions")